package com.bdzl.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.bdzl.api.util.QCloudOCRUtil;
import com.bdzl.pub.config.Constant;
import com.bdzl.pub.entity.data.UserInfoDO;
import com.bdzl.pub.util.file.OssUploadFileUtil;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.vo.data.IdCardVO;
import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname QCloundOCRController
 * @Description TODO
 * @Date 2021-03-11 10:15
 * @Created by wjg
 * OCR 对外接口
 */
@RestController
@RequestMapping("/ocr")
public class QCloundOCRController {
    /**
     * 0: 人面像
     * 1：国徽面
     * @param idCardVO 身份证照片vo类
     * @return
     */
    @RequestMapping("/idCardOCR")
    @ResponseBody
    public R idCardOCR(@RequestBody IdCardVO idCardVO){
        if (idCardVO ==null){
            return R.error("请上传身份证照片！");
        }
        String frontUrl = null;
        String backUrl = null;
        Map<String,Object> resMap = new HashMap<>();
        try {
            if ("front".equals(idCardVO.getType())){
                // 创建临时文件夹
                frontUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(idCardVO.getUrl(), Constant.FDD_FACEDE_TYPE);
                List<UserInfoDO> userInfoDOList = new ArrayList<>() ;
                QCloudOCRUtil.getIDDos(new String[]{frontUrl} , 0 , userInfoDOList );
                //删除临时目录文件
                if (frontUrl!=null){
                    ArrayList<String> strings = Lists.newArrayList(frontUrl);
                    OssUploadFileUtil.bathRemove(strings);
                }
                if (userInfoDOList != null && userInfoDOList.size()>0){
                    UserInfoDO userInfoDO = userInfoDOList.get(0);
                    if ("front".equals(idCardVO.getType())) {
                        frontUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(idCardVO.getUrl(), Constant.IDCARD_PATH);
                        userInfoDO.setConCardPeople(frontUrl);
                    }
                    System.out.println(userInfoDO);
                    System.out.println(userInfoDO.getConBirth());
                    resMap.put("userInfo",userInfoDO);
                }
            }else if ("back".equals(idCardVO.getType())) {
                UserInfoDO userInfoDO = new UserInfoDO();
                backUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(idCardVO.getUrl(), Constant.IDCARD_PATH);
                userInfoDO.setConCardEmblem(backUrl);
                resMap.put("userInfo",userInfoDO);
            }
            return R.ok(resMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error("识别失败!");
    }

    /**
     * 营业执照OCR
     */
    @PostMapping("/blicenseOCR")
    @ResponseBody
    public String  blicenseOCR (@RequestBody String file) throws Exception{
        //1.上传到阿里云
        file = OssUploadFileUtil.RandomFileName.uploadToOSS(file, "loan/ent/ent_license");
        String response = QCloudOCRUtil.getBusinessMsg(file);
        JSONObject jsonObject = JSONObject.parseObject(response);
        jsonObject.put("url",file);
        response = jsonObject.toJSONString();
        System.out.println(response);
        return response;
    }




}
